﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;

namespace ACM
{
    public partial class SaleKeyIn : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnkCheckVIN_Click(object sender, EventArgs e)
        {
            GetCarAdvanceCommand cmd = new GetCarAdvanceCommand(txtVIN.Text); //Check Vin
            cmd.Execute();
            if (cmd.Result.Count > 0)
            {
                txtSeries.Text = cmd.Result[0].car_series;
                txtSubSeries.Text = cmd.Result[0].car_sub_series;
                txtModelSuffix.Text = cmd.Result[0].car_model_suffix;
                txtSaleDate.Text = cmd.Result[0].sale_date.ToString("dd/MM/yyyy");

                if (cmd.Result[0].user_id == null || cmd.Result[0].user_id == 0)
                {
                    btnSave.Enabled = true;
                    Status.Hide();
                }
                else
                {
                    btnSave.Enabled = false;
                    string msg = "VIN ดังกล่าวถูกลงทะเบียนแล้ว ";

                    GetUserCommand ucmd = new GetUserCommand(cmd.Result[0].user_id);
                    ucmd.Execute();
                    if (ucmd.Result != null)
                    {
                        msg += "โดย " + ucmd.Result.dealer_name;
                    }

                    Status.ShowWarning(msg);
                }
                
            }
            else
            {
                Status.ShowError("ไม่พบ VIN ดังกล่าวในระบบ");
                btnSave.Enabled = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            AddEditCarAdvanceCommand cmd = new AddEditCarAdvanceCommand(CurrentUser.id, CurrentUser.dealer_code, txtVIN.Text);
            cmd.Execute();

            if (Convert.ToInt16(cmd.Result) > 0)
            {
                Status.ShowSuccess("บันทึกข้อมูลเรียบร้อยแล้ว");
                clearForm();
            }
            else
                Status.ShowError("เกิดข้อผิดพลาดในการบันทกข้อมูล กรุณาติดต่อผู้ดูแลระบบ");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            clearForm();
            Status.Hide();
        }

        private void clearForm()
        {
            txtVIN.Text = "";
            txtSeries.Text = "";
            txtSubSeries.Text = "";
            txtModelSuffix.Text = "";
            txtSaleDate.Text = "";
            btnSave.Enabled = false;
        }
    }
}